package com.dsj.gxrd.modules.book.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.book.domain.dto.RdFileDto;
import com.dsj.gxrd.modules.book.domain.entity.RdFile;
import com.dsj.gxrd.modules.book.enums.FileRelationTypeEnum;
import com.dsj.gxrd.modules.book.mapper.RdFileMapper;
import com.dsj.gxrd.modules.book.service.IRdFileService;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【文件处理接口】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-11
 */
@Service
public class RdFileServiceImpl extends ServiceImpl<RdFileMapper, RdFile> implements IRdFileService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveFileBatch(List<RdFileDto> fileDtoList, Long relationId, FileRelationTypeEnum relationType) {
        if (CollectionUtils.isEmpty(fileDtoList) || relationId == null) {
            return;
        }
        List<RdFile> collect = fileDtoList.stream().map(item -> new RdFile(item.getFileName() == null ? "" : item.getFileName(), item.getUrl(), relationId, relationType.getType(), 0))
                .collect(Collectors.toList());
        this.saveBatch(collect);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RdFileDto> listFiles(Long relationId, FileRelationTypeEnum relationType) {
        if (relationId == null || relationType == null) {
            return new ArrayList<>();
        }
        List<RdFile> list = this.lambdaQuery()
                .eq(RdFile::getRelationId, relationId)
                .eq(RdFile::getRelationType, relationType.getType())
                .list();
        return BeanUtils.copyProperties(list, RdFileDto.class);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFileBatch(List<RdFileDto> fileList, Long relationId, FileRelationTypeEnum relationType) {
        this.deleteFilesByRelationId(relationId, relationType);
        this.saveFileBatch(fileList, relationId, relationType);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteFilesByRelationId(Long relationId, FileRelationTypeEnum relationType) {
        if (relationId == null || relationType == null) return;
        Long i = this.lambdaQuery()
                .eq(RdFile::getRelationId, relationId)
                .eq(RdFile::getRelationType, relationType.getType())
                .count();
        if (i != null && i > 0) {
            this.lambdaUpdate()
                    .eq(RdFile::getRelationId, relationId)
                    .eq(RdFile::getRelationType, relationType.getType())
                    .remove();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFilesByRelationIds(List<Long> relationIds, FileRelationTypeEnum relationType) {
        if (CollectionUtils.isEmpty(relationIds) || relationType == null) return;
        Long count = this.lambdaQuery()
                .eq(RdFile::getRelationType, relationType.getType())
                .in(RdFile::getRelationId, relationIds)
                .count();
        if (count != null && count > 0) {
            this.lambdaUpdate()
                    .eq(RdFile::getRelationType, relationType.getType())
                    .in(RdFile::getRelationId, relationIds)
                    .remove();
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteFilesByFileIds(Long relationId, FileRelationTypeEnum relationType, List<Long> fileList) {
        if (relationId == null || relationType == null || CollectionUtils.isEmpty(fileList)) return;
        //如果要删除的数量和查询出的数量不一致，不执行删除
        Long i = this.lambdaQuery()
                .eq(RdFile::getRelationId, relationId)
                .eq(RdFile::getRelationType, relationType.getType())
                .in(RdFile::getRdFileId, fileList)
                .count();
        if (i == null || i == 0 || i != fileList.size()) return;
        this.lambdaUpdate()
                .in(RdFile::getRdFileId, fileList)
                .remove();
    }

    @Override
    public Map<Long, List<RdFileDto>> listFilesMap(List<Long> relationIds, FileRelationTypeEnum relationType) {
        if (relationIds == null || relationIds.isEmpty()) {
            return new HashMap<>(0);
        }
        List<RdFile> list = this.lambdaQuery()
                .eq(RdFile::getRelationType, relationType.getType())
                .in(RdFile::getRelationId, relationIds)
                .list();
        return list.stream().collect(
                Collectors.groupingBy(RdFile::getRelationId, Collectors.mapping(item -> BeanUtils.copyProperties(item, RdFileDto.class), Collectors.toList()))
        );
    }
}
